package edu.zut.cs.software.cms.module.course.web.spring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.zut.cs.software.cms.base.web.spring.controller.GenericController;
import edu.zut.cs.software.cms.module.course.domain.Course;
import edu.zut.cs.software.cms.module.course.service.CourseManager;

/**
 * 
 * @author 24372
 *
 */
@Controller
@RequestMapping("/course")
public class CourseController extends GenericController<Course, Long, CourseManager> {

	CourseManager courseManager;

	@RequestMapping(method = RequestMethod.GET, value = "/index.html")
	public String index() {
		String result = "/course/index";
		return result;
	}

	@Autowired
	public void setCourseManager(CourseManager courseManager) {
		this.courseManager = courseManager;
		this.manager = this.courseManager;
	}

	@RequestMapping(value = "getCourseById")
	@ResponseBody
	public List<Course> getCourseById(String code) {
		return courseManager.FindByFullname(code);
	}

	// 删除
	@RequestMapping(value = "DeleteById")
	@ResponseBody
	public String DeleteById(long id) {
		if (courseManager.DeleteById(id))
			return "delete success";
		else
			return "delete failure";
	}

	// 修改
	@RequestMapping("/modifyOneCourse")
	@ResponseBody
	public String modifyOneScore(long id, String code, String name, String teaching, String credit) {
		if (courseManager.modifyById(id, code, name, teaching, credit) == true)
			return "modify success!";
		else
			return "modify failed!";
	}



	@RequestMapping("/CreateCourse")
	@ResponseBody
	public String CreateOne(String name, String teaching, String identity) {
		String flag = "";
		Course course = new Course();
		if (courseManager.IfExist(name))// 返回true
		{
			flag = "true";
			course.setName(name);
			course.setTeaching(teaching);
			courseManager.save(course);
		} else {
			flag = "false";
		}
		return flag;
	}
}